﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using AForge.Video;
using AForge.Imaging;
using AForge.Math;
using AForge.Controls;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;

namespace demo1
{
    public partial class main : Form
    {
        public FilterInfoCollection video_dev;
        public main()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(video_dev[cB_dev_list.SelectedIndex].MonikerString);
            if (btn_open_camera.Text == "打开摄像头")
            {
                btn_picture.Enabled = true;
                btn_video.Enabled = true;
                btn_pixel.Enabled = true;
                btn_open_camera.Text = "关闭摄像头";
                video_player.SignalToStop();
                video_player.WaitForStop();
                video_player.VideoSource = videoSource;
                video_player.Start();
            }else if(btn_open_camera.Text == "关闭摄像头")
            {
                btn_picture.Enabled = false;
                btn_video.Enabled = false;
                btn_pixel.Enabled = false;
                btn_open_camera.Text = "打开摄像头";
                video_player.Stop();
            }
            
        }

        private void main_Load(object sender, EventArgs e)
        {
            video_dev = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (video_dev.Count == 0)
            {
                cB_dev_list.Items.Add("No local capture devices");
                video_dev = null;
            }
            else
            {
                foreach (FilterInfo dev in video_dev)
                {
                    cB_dev_list.Items.Add(dev.Name);
                }
                cB_dev_list.SelectedIndex = 0;
            }
        }

        private void btn_resize_Click(object sender, EventArgs e)
        {

        }

        private void cB_dev_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            cB_pixel.Items.Clear();
            VideoCaptureDevice videoSource = new VideoCaptureDevice(video_dev[cB_dev_list.SelectedIndex].MonikerString);
            foreach (VideoCapabilities info in videoSource.VideoCapabilities)
            {
                cB_pixel.Items.Add(info.FrameSize);
            }
            cB_pixel.SelectedIndex = 0;
        }

        private void btn_pixel_Click(object sender, EventArgs e)
        {
            Size info = (Size)cB_pixel.SelectedItem;
            //video_player.Size = new Size(info.Width, info.Height);
            VideoCaptureDevice videoSource = new VideoCaptureDevice(video_dev[cB_dev_list.SelectedIndex].MonikerString);
            videoSource.VideoResolution = videoSource.VideoCapabilities[cB_pixel.SelectedIndex];
        }

        private void btn_picture_Click(object sender, EventArgs e)
        {
            Bitmap bitmap = video_player.GetCurrentVideoFrame();
            string fileName = "Camera-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".jpg";//DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff") + ".jpg";
            bitmap.Save(@".\" + fileName, ImageFormat.Jpeg);
            bitmap.Dispose();
        }
    }
}
